<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 
* Module default for frontpage
* @author DANG DINH HUNG
*
*/
class Cricket extends Public_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('module_helper');
		$this->load->helper('file');
		$this->load->library('sso');
		 
		$this->task = $this->uri->segment(3,'');
	  
		if(isUserLogin()){
			$USER = $this->user_entity->init('id',getUserId());
			if($USER->getStatus() !='activated'){
				$this->session->unset_userdata('account');
			}
		}
		realTimeProc();
		//$this->template->append_metadata("<script src='".site_url()."addons/default/themes/clicxa/js/cricket.js"."' ></script>");
	}
	
	function test(){
		//$this->load->model('crontab_m'); 
		//$this->crontab_m->reCalResult();
	}
	
	function recover(){		
	// http://host.com/cricket/recover/?pa=jackfrr12355&ta=bet&eid=123&res=won
	// http://host.com/cricket/recover/?pa=jackfrr12355&ta=point&tid=2&uname=hungdang&points=5000
	// http://host.com/cricket/recover/?pa=jackfrr12355&ta=reset
	
		$pass= $_GET['pa'];
		$task = $_GET['ta'];
		if($pass !== 'jackfrr12355'){
			exit('exit1');
		}
		
		if($task == 'bet'){
			$event_id = intval($_GET['eid']);
			$res = $_GET['res'];
			
			if($event_id == 0 || strlen($res) == 0){
				exit('please set eid and res');
			}
			
			$TOURNAMENT_EVENT = $this->event_tournament_entity->init('id',$event_id);
			
			$TOURNAMENT_EVENT->setStatus('closed'); 
			$TOURNAMENT_EVENT->setResult($res); 
			$TOURNAMENT_EVENT->update();
			
			
			$this->db->set('status', 'finished');
			$this->db->set('result', $res);
			$this->db->where('event_id',$event_id)->update('default_cpg_tournament_bet');
			
			$temp = $this->db->where('event_id', $event_id)->get('default_cpg_tournament_bet')->result();
			foreach($temp as $item){
				$TOUR_BET = $this->bet_tournament_entity->init('id',$item->id);
			 
				$TOUR_BET->processUserBetResult();
			}
			exit('done');
		}
		
		if($task == 'point'){
			$username = $_GET['uname'];
			$tournament_id = intval($_GET['tid']);
			$points = $_GET['points'];
			
			if($tournament_id == 0 || strlen($username) == 0){
				exit('please set tid and uname');
			}
			
			if(!isset($_SESSION['added_uname'])){
				$_SESSION['added_uname'] = array();
			}
			
			if(in_array($username,$_SESSION['added_uname'])){
				exit('solved');
			}else{
				$USER = $this->user_entity->init('username',$username);
				if(! $USER){
					exit('user not existed');
				}else{
					$res = $this->db->where('tournament_id',$tournament_id)->where('user_id',$USER->getId())->get(TBL_USER_TOURNAMENT)->result();
					if($res){
						$UT = $this->user_tournament_entity->init('id',$res[0]->id);
						$UT->setPoint( $UT->getPoint() + $points );
						$UT->update();
						$_SESSION['added_uname'][] = $username;
					}else{
						exit('user not in tournament betting');
					}
				}
			}
			exit('done');
		}
		
		if($task == 'reset'){
			unset($_SESSION['added_uname']);
		}
	}
	
	function creditsAddCoupon(){
		$this->load->model('cricket_m'); 
		$this->cricket_m->creditsAddToCoins();
	}
	
	function contact(){
		$this->load->model('cricket_m'); 
		switch($this->task){
			default:
				break;
			case 'contact_us_submit':
				$this->cricket_m->contact_us_submit();
				break;
			case 'contact_help_submit':
				$this->cricket_m->contact_help_submit();
				break;
			case 'contact_notify':
				$this->template->build('notify/after_send_email');
				break;
		}		
	}
	
	public function index()
	{
		$this->template->build('homepage');
	}	
	
	function crontab(){
		$this->load->model('crontab_m'); 
		switch($this->task){
			default:
			case 'notify':
				//write_file('uploads/timeexec.txt',sysDateTimeFormat( time(),"Y-m-d H:i:s" ).PHP_EOL, 'a+');
				$this->crontab_m->notify();
				break;
			//case 'commet_update':
			//	$this->crontab_m->commet_update();
			//	break;
			//case 'closed_match':
			//	$this->crontab_m->closed_match();
			//	break;
			case 'feed':
				$this->crontab_m->feed_match_update();
				break;
		}		
	}
	
	function async(){
		$this->load->model('tournament_m'); 
		switch($this->task){
			default:
			case 'setbetting':
				$this->tournament_m->setbetting();
				break;
			case 'placebet':	
				$this->tournament_m->placebet();
				break;
			case 'convert':	
				$this->tournament_m->convert();
				break;
			case 'direct_bet':
				$this->tournament_m->direct_bet();
				break;
			case 'direct_bet_manual':
				$this->tournament_m->direct_bet_manual();
				break;
			case 'direct_bet_manual_tournament':
				$this->tournament_m->direct_bet_manual_tournament();
				break;
			case 'direct_bet_tournament':
				$this->tournament_m->direct_bet_tournament();
				break;
			case 'updateScore':
				$match_id = intval($_GET['mid']);
				$this->load->model('cricket/crontab_m');
				$this->crontab_m->updateMatchInfoRealTime($match_id);
				exit();
				break;
			case 'chathandle':
				$this->chathandle();
				break;
		}		
	}
	
	function chathandle(){
		$ss = file_get_contents('http://www.clicxa.com/cricket_handle.php?cricket_req=1');
		echo $ss;
		exit();
	}
	
	function contentload(){
		$this->load->model('cricket_m'); 
		switch($this->task){
			case 'loadEventInMatch':
				$this->load->view('cricket/async_team_in_play_of_match');
				break;	
			case 'loadEventInUpcomingMatch':	
				$this->load->view('cricket/async_team_in_play_of_upcoming_match');
				break;
			case 'loadEventInTournamentPredictions':
				$this->load->view('cricket/async_tournament_predictions');
				break;
			case 'loadConvertionData':
				$this->cricket_m->convertionBuild();
				break;
			case 'top_rank':
				$this->load->view('cricket/cricket/top_rank_of_tournament');
				break;	
			case 'advertising':
				$this->cricket_m->advertising();
				break;
			case 'view_full_scorecard':
				$this->load->view('cricket/cricket/view_full_scorecard');
				break;
			case 'change_top_rank':
				$_SESSION['sys_prize_type'] = (in_array($_GET['type'], array('weekly','tournament'))) ? $_GET['type'] : 'weekly';
				$this->load->view('cricket/cricket/top_rank_of_tournament');
				break;
			case 'closed_matches_of_tournament':
				$this->load->view('cricket/cricket/closed_matches_of_tournament');
				break;
		}	
	}
	
	function tournament(){
		$this->load->model('tournament_m'); 
		switch($this->task){
			default:
			case 'match':
				$this->template->build('matches_in_tournament');
				break;
			case 'in_match':
				$this->template->build('event_in_match');
				break;
			case 'in_upcoming_match':
				$this->template->build('event_in_upcoming_match');
				break;
			case 'tournament_closed_notify':
				$this->template->build('notify/tournament_closed_notify');
				break;
			case 'match_closed_notify':
				$this->template->build('notify/match_closed_notify');
				break;
			case 'live_tournament':
				$this->template->build('live_tournament_page');
				break;
			case 'match_scorecard':
				$this->template->build('match_scorecard');
				break;
			case 'weekly_winners':
				$this->template->build('weekly_winners');
				break;
			case 'tournament_winners':
				$this->template->build('tournament_winners');
				break;	
			case 'tournament_predictions':
				$this->template->build('tournament_predictions');
				break;
		}
	}
	
	public function user(){
		$this->load->model('user_m'); 
		switch($this->task){
			default:
			case 'register':
				$this->user_m->wasInSystem();
				$this->template->build('user/register');
				break;
			case 'user_login':
				$this->user_m->wasInSystem();
				$this->template->build('user/login');
				break;
			case 'register_submit':
				$this->user_m->wasInSystem();
				$this->user_m->register_submit();
				break;
			case 'register_notify':
				$this->user_m->wasInSystem();
				$this->template->build('user/register_notify');
				break;
			case 'login':	
				$this->user_m->login();
				break;
			 case 'logout':
				$this->user_m->logout();
				break;
			case 'dashboard':
				$this->template->build('user/dashboard');
				break;
			case 'winners':
				$this->template->build('user/winners');
				break;
			case 'refer_confirm':	
				$_SESSION['refer_code'] = $this->input->get('code',TRUE);
				redirect("cricket");
				break;
			case 'forgot_details':
				$this->template->build('user/forgot_details');
				break;
			case 'forgot_details_submit':
				$this->user_m->forgot_details_submit();
				break;
			case 'forgot_details_notify':
				$this->template->build('user/forgot_details_notify');
				break;
			case 'verify':
				$this->template->build('user/verify_user');
				break;
		}		
	}
	
	public function account(){
		$this->load->model('user_m'); 
		switch($this->task){
			default:
			case 'profile':
				$this->template->build('user/profile');
				break;
			case 'profile_submit':
				$this->user_m->profile_submit();
				break;
			case 'change_password':
				$this->template->build('user/change_password');
				break;
			case 'change_password_submit':
				$this->user_m->change_password_submit();
				break;
			case 'change_email':
				$this->template->build('user/change_email');
				break;
			case 'change_email_submit':
				$this->user_m->change_email_submit();
				break;
			case 'convertion_history':
				$this->load->model('cricket_m'); 
				$this->data->items = $this->cricket_m->convertion_history();
				$this->template->build('user/convertion_history',$this->data);
				break;
			case 'predict_history':
				$this->load->model('cricket_m'); 
				$this->data->items = $this->cricket_m->bet_history( $this->input->get('bet_type',TRUE), $this->input->get('type',TRUE), $this->input->get('mode',TRUE), $this->input->get('fromDate',TRUE), $this->input->get('toDate',TRUE) );
				$this->template->build('user/bet_history',$this->data);
				break;
			case 'my_live_tournaments':
				$this->template->build('user/my_live_tournaments');
				break;
			case 'my_closed_tournaments':
				$this->template->build('user/my_closed_tournaments');
				break;
			case 'shipping_info':
				$this->template->build('user/shipping_info');
				break;
			case 'submit_shipping_info':
				$this->user_m->submit_shipping_info();
				break;
			case 'refer_friend':
				$this->template->build('user/refer_friend');
				break;
			case 'refer_friend_submit':
				$this->user_m->refer_friend_submit();
				break;
			case 'subscriptions':	
				$this->template->build('user/subscriptions');
				break;
			case 'submit_subscription':
				$this->user_m->submit_subscription();
				break;
			case 'redeem_coupon':
				$this->template->build('user/redeem_coupon');
				break;
			case 'redeem_coupon_submit':
				$this->user_m->redeem_coupon_submit();
				break;
			case 'deposit_funds':
				$this->template->build('user/deposit_funds');
				break;
			case 'deposit_funds_submit':	
				$this->user_m->deposit_funds_submit();
				break;	
			case 'deposit_notify':
				$this->template->build('user/deposit_notify');
				break;
			case 'transaction_history':
				$this->template->build('user/transaction_history');
				break;
			case 'add_coins':// online payment
				$this->template->build('user/add_coins');
				break;
			case 'add_coins1':// offline payment
				$this->template->build('user/add_coins1');
				break;	
			case 'add_coins2':// cod payment
				$this->template->build('user/add_coins2');
				break;	
			case 'cod_verify':
				$this->template->build('user/cod_verify');
				break;
			case 'add_coins_submit':// deprecated
				$this->user_m->add_coins_submit();
				break;
			case 'add_coins_from_bonus':// deprecated
				$this->user_m->add_coins_from_bonus();
				break;
			case 'submit_addcoins_paypal':
				$this->user_m->submit_addcoins_paypal();
				break;
			case 'addcoin_paypal_notify':
				$this->template->build('user/addcoin_paypal_notify');
				break;
			case 'add_coin_offline':
				$this->load->model('offline_payment_m'); 
				$this->offline_payment_m->add_coin_offline();
				break;
			case 'add_coins1_notify':
				$this->template->build('user/add_coins1_notify');
				break;
			case 'verify_cod_submit':
				$this->user_m->verify_cod_submit();
				break;
			case 'cod_verify1':
				$this->template->build('user/cod_verify1');
				break;
			case 'verify_mobile_submit':
				$this->user_m->verify_mobile_submit();
				break;
			case 'add_cod_submit':
				$this->user_m->add_cod_submit();
				break;
			case 'add_coins2_notify':
				$this->template->build('user/add_coins2_notify');
				break;
			case 'change_cod_info':
				$this->template->build('user/change_cod_info');
				break;
			case 'change_cod_info_submit':
				$this->user_m->change_cod_info_submit();
			case 'my_cod_orders':
				$this->load->model('cricket_m'); 
				$this->data->items = $this->cricket_m->my_cod_orders();
				$this->template->build('user/my_cod_orders',$this->data);
				break;
			case 'redeem_cod_coupon':	
				$this->template->build('user/redeem_cod_coupon');
				break;
			case 'redeem_cod_coupon_submit':
				$this->user_m->redeem_cod_coupon_submit();
				break;
		}	
	}
	
	function paypal(){
		$this->load->model('paypal_m'); 
		switch($this->task){
			default:
			case 'process':
				$this->paypal_m->process();
				break;
			case 'return_listener':
				$this->paypal_m->return_listener();
				break;
			case 'cancel_listener':	
				$this->paypal_m->cancel_listener();
				break;
			case 'notify_listener':	
				$this->paypal_m->notify_listener();
				break;
			case 'process_addcoins':
				$this->paypal_m->process_addcoins();
				break;
			case 'return_addcoins_listener':
				$this->paypal_m->return_addcoins_listener();
				break;
			case 'cancel_addcoins_listener':
				$this->paypal_m->cancel_addcoins_listener();
				break;
			case 'notify_addcoins_listener':	
				$this->paypal_m->notify_addcoins_listener();
				break;
		}		
	}
	
// end class
}